import {
	mapGetters,
	mapActions
} from 'vuex'
var QQMapWX = require('@/utils/qqmap-wx-jssdk/qqmap-wx-jssdk.min.js');
var qqmapsdk;
export default {
	data() {
		return {
			//设置默认的分享参数
			//如果页面不设置share，就触发这个默认的分享
			share: {
				title: '一站式旅游出行服务',
				path: '/pages/tabbar/home',
				imageUrl: 'https://imgs.2855855.com/imgs/other/share.png',
				desc: '',
				content: ''
			},
			// #ifdef MP-WEIXIN
			wxbtn: uni.getMenuButtonBoundingClientRect(), // 微信小程序按钮
			// #endif
			statusbar_height: uni.getSystemInfoSync().statusBarHeight, // 状态栏高度
			page_height: uni.getSystemInfoSync().windowHeight,
			shopImgUrl: '', // 商城 网络图片路径
			wwwImgUrl: '', // 网络图片路径
			www: 'https://imgs.2855855.com/', // 网络静态图片路径
		}
	},
	onShareAppMessage(res) {
		return {
			title: this.share.title + '?inviteCode=' + this.getCustomerInfo.inviteCode,
			path: this.share.path + '?inviteCode=' + this.getCustomerInfo.inviteCode,
			imageUrl: this.share.imageUrl,
			desc: this.share.desc,
			content: this.share.content,
			success(res) {
				uni.showToast({
					title: '分享成功'
				})
			},
			fail(res) {
				uni.showToast({
					title: '分享失败',
					icon: 'none'
				})
			}
		}
	},
	computed: {
		...mapGetters(['getCustomerInfo'])
	},
	methods: {
		...mapActions(['upcustomerInfo']),
		// 用户授权微信通知
		submitWxNotifyConfirm(templateId, confirmType) {
			this.$http.get(this.$api.wxNotifyConfirm, {
				templateId, // 	通知的模版ID
				confirmType // 字符串 accept , reject , ban
			}).then(res => {
				if (res.code === 200) {}
			})
		},
		// 跳转页面 不携带参数
		pageJumps(url, zhu) {
			console.log(url);
			if (zhu) {
				uni.switchTab({
					url: url
				})
			} else {
				uni.navigateTo({
					url: url
				})
			}
		},
		// 页面返回上级
		pageBack(n = 1) {
			uni.navigateBack({
				delta: n
			});
		},
		// 用户积分信息
		async getcustomerInfo() {
			await this.$http.get(this.$api.customerInfo).then(res => {
				if (res.code === 200) {
					this.upcustomerInfo(res.data)
				}
			})
		},
		// // h5 给后台换定位
		async exchangeDingwei(latitude, longitude) {
			let data
			// 'https://2855855.com/front/miniapp/vehicle/customerLocationInfo'
			await this.$http.get(this.$api.customerLocationInfo, {
				lat: latitude,
				lng: longitude,
			}).then(res => {
				if (res.code === 200) {
					data = res.data
				} else {
					uni.showToast({
						title: '定位失败',
						icon: 'none'

					})
				}
			})
			return data
		},
		mixinInitMap() {
			qqmapsdk = new QQMapWX({
				key: 'C7JBZ-VQD6D-BVZ4C-PHTJO-HNPSF-ORFT5'
			});
			this.getLocation()
		},
		getLocation() {
			console.log("定位中...");
			let _this = this
			uni.getLocation({
				type: 'gcj02',
				success: async function(res) {
					console.log('定位数据...', res);
					let {
						latitude,
						longitude
					} = res
					let data = await _this.exchangeDingwei(latitude, longitude)
					let addressName = data.result.ad_info.city
					_this.UpCustomerUploadLocation(addressName, latitude, longitude)
				}
			});
		},
		async UpCustomerUploadLocation(addressName, latitude, longitude) {
			await this.$http.get(this.$api.customerUploadLocation + '?city=' + addressName + '&lat=' + latitude + '&lng=' +
				longitude).then(res => {
				if (res.code === 200) {
					// uni.showToast({
					// 	title: '上传坐标',
					// 	icon: 'none'
					// })
				}
			})
		},
		// 图片加载错误[列表]
		onImgLoadErr(data, attr) {
			this.$set(data, attr, '/profile/upload/2020/12/22/7e168b8e596d860448540fb4ad53658d.png')
		},
		// 富文本图片适配
		parseRichImgStyle(content) {
			if (typeof content === "string" && content !== "") {
				let newContent = content.replace(/<img[^>]*>/gi, (match, capture) => {
					match = match.replace(/style="[^"]+"/gi, "").replace(/style='[^']+'/gi, "");
					match = match.replace(/width="[^"]+"/gi, "").replace(/width='[^']+'/gi, "");
					match = match.replace(/height="[^"]+"/gi, "").replace(/height='[^']+'/gi, "");
					return match;
				});
				newContent = newContent.replace(/style="[^"]+"/gi, (match, capture) => {
					match = match.replace(/width:[^;]+;/gi, "max-width:100%;").replace(/width:[^;]+;/gi, "max-width:100%;");
					return match;
				});
				newContent = newContent.replace(/<br[^>]*\/>/gi, "");
				newContent = newContent.replace(/\<img/gi,
					"<img style=\"max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;\"");
				return newContent;
			} else {
				return "";
			}
		},
		// 返回上一页
		navBack() {
			uni.navigateBack()
		},
		// 获取上一页面路径
		getLastPagePath() {
			const pages = getCurrentPages();
			const page = pages[pages.length - 2];
			return page ? page.route : ''
		},
		// 处理销量数据
		handleSales(sales) {
			const num = parseInt(sales)
			if (!isNaN(num) && num > 0) {
				return num >= 10000 ? (num / 10000).toFixed(1) + '万' : num
			} else {
				return 0
			}
		},
		// 生成随机字符
		randomString(str_length) {
			const str_temp = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz0123456789";
			const str_temp_len = str_temp.length;
			const str_len = str_length || 16;
			let str = "";
			for (let i = 0; i < str_temp_len; i++) {
				str += str_temp.charAt(Math.floor(Math.random() * str_len));
			}
			return str
		},
		// 任何类型转换成数字
		trunNumber(data) {
			return isNaN(Number(data)) ? 0 : Number(data)
		}
	},
	// 商品规格1
	mpData(arr) {
		if (Array.isArray(arr) && arr.length > 0) {
			const data = {};
			const arr1 = []; // specification字段名称
			if (typeof arr[0].spData === "string" && arr[0].spData !== "") {
				try {
					const spData = JSON.parse(arr[0].spData)
					if (Array.isArray(spData)) {
						spData.map(item2 => {
							// 未收集的specification名称
							const index = arr1.findIndex(i => i === item2.specification)
							// console.log(index)
							if (index === -1) {
								arr1.push(item2.specification)
								data[item2.specification] = [{
									...arr[0],
									dName: item2.value,
									picUrl: item2.picUrl,
									selected: false
								}]
							} else {
								data[item2.specification].push({
									...arr[0],
									dName: item2.value,
									picUrl: item2.picUrl,
									selected: false
								})
							}
			
						})
						// console.log(data)
					}
				} catch (e) {
					console.log(`${index1}数据错误`);
				}
			}
			return data;
		} else {
			return {}
		}
	},
	// 商品规格2
	mpData2(arr) {
		if (Array.isArray(arr)) {
			const arrx = []
			arr.map(item => {
				if (item.spDetail) {
					try {
						const spDetail = JSON.parse(item.spDetail)
						if (Array.isArray(spDetail)) {
							arrx.push({
								...item,
								dName: spDetail.join('-'),
							})
						}
					} catch (e) {
						console.log(`${dataName}数据错误`);
					}
				}
			})
			return arrx;
		} else {
			return []
		}
	},
}
